<template>
  <div>
    <div>
      <el-input
        v-model="jl.name"
        prefix-icon="el-icon-plus"
        size="mini"
        placeholder="添加职称..."
        style="width: 300px"
      ></el-input>
      <el-select
        v-model="jl.titleLevel"
        clearable
        placeholder="职称等级"
        size="mini"
        style="margin: 8px"
      >
        <el-option
          v-for="item in titleLevel"
          :key="item"
          :label="item"
          :value="item"
        >
        </el-option>
      </el-select>
      <el-button icon="el-icon-plus" type="primary" size="mini" @click="addJob"
        >添加</el-button
      >
    </div>
    <el-button
      type="danger"
      :disabled="multipleSelection == 0"
      size="mini"
      @click="deleteBatch"
      >批量删除</el-button
    >
    <div style="margin-top: 8px">
      <el-table
        :data="jobLevel"
        size="mini"
        :header-cell-style="{background:'#FAFAFA'}"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        >>
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="编号" width="100"> </el-table-column>
        <el-table-column prop="name" label="姓名" width="120">
        </el-table-column>
        <el-table-column prop="titleLevel" label="职称等级"> </el-table-column>
        <el-table-column prop="createDate" label="创建时间"> </el-table-column>
        <el-table-column label="是否启用">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
            <el-tag type="danger" v-else>未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>


    <el-dialog title="修改职称" :visible.sync="dialogVisible" width="30%">
      <el-form ref="updataJob" :model="updataJob" label-width="80px">
        <el-form-item label="职称名称">
          <el-input v-model="updataJob.name" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="职称等级">
          <el-select v-model="updataJob.titleLevel" size="mini" clearable placeholder="请选择职称等级">
            <el-option
                v-for="item in titleLevel"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-switch v-model="updataJob.enabled" active-text="已启用" inactive-text="未启用"></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="updateJob"
          >确 定</el-button>
      </span>
    </el-dialog>

<!-- 
    <el-dialog title="修改职称信息" :visible.sync="dialogVisible" width="30%">
      <div>
        <el-row>
          <el-col :span="5" class="el_col">职称名称:</el-col>
          <el-col :span="19"
            ><el-input size="mini" v-model="updataJob.name"></el-input
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="5" class="el_col">职称等级:</el-col>
          <el-col :span="19">
            <el-select
              v-model="updataJob.titleLevel"
              clearable
              placeholder="职称等级"
              size="mini"
              style="margin: 0px"
            >
              <el-option
                v-for="item in titleLevel"
                :key="item"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5" class="el_col">是否启用</el-col>
          <el-col :span="19">
            <el-switch
              v-model="updataJob.enabled"
              active-text="已启用"
              inactive-text="未启用">
            </el-switch>
          </el-col>
        </el-row>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="updateJob"
          >确 定</el-button
        >
      </span>
    </el-dialog> -->
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      jl: {
        name: "",
        titleLevel: "",
      },
      jobLevel: [],
      titleLevel: ["正高级", "副高级", "中级", "初级", "员级"],
      dialogVisible: false,
      updataJob: {
        id: "",
        name: "",
        titleLevel: "",
        enabled: false
      },
      multipleSelection: [],
    };
  },
  methods: {
    deleteBatch() {
      let ids = [];
      this.multipleSelection.forEach((item) => {
        ids.push(item.id);
      });
      this.$confirm(
        "此操作将永久删除【" + ids.length + "】条职位信息, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.deleteRequset("/system/basic/job/", ids).then((resp) => {
            this.initJobLevel();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(val) {
      console.log(val);
      this.multipleSelection = val;
    },
    updateJob() {
      if (!this.updataJob.name) {
        this.$message.error("职称名称不能为空!");
        return false;
      }
      if (!this.updataJob.titleLevel) {
        this.$message.error("请选择职称级别");
        return false;
      }
      this.putRequset("/system/basic/job/", this.updataJob).then((resp) => {
        this.initJobLevel();
        this.dialogVisible = false;
      });
    },
    addJob() {
      if (!this.jl.name) {
        this.$message.error("职称名称不能为空!");
        return false;
      }
      if (!this.jl.titleLevel) {
        this.$message.error("请选择职称级别");
        return false;
      }
      this.postRequset("/system/basic/job/", this.jl).then((resp) => {
        if (resp) {
          this.initJobLevel();
          this.jl.name = "";
          this.jl.titleLevel = "";
        }
      });
    },
    handleEdit(index, data) {
      this.updataJob.id = data.id;
      this.updataJob.name = data.name;
      this.updataJob.titleLevel = data.titleLevel;
      this.updataJob.enabled = data.enabled;
      this.dialogVisible = true;
    },
    handleDelete(index, data) {
      this.$confirm(
        "此操作将永久删除该【" + data.name + "】职称, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          let ids = [];
          ids.push(data.id);
          this.deleteRequset("/system/basic/job/", ids).then((resp) => {
            this.initJobLevel();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    initJobLevel() {
      this.getRequset("/system/basic/job/").then((resp) => {
        if (resp) {
          this.jobLevel = resp;
        }
      });
    },
  },
  mounted() {
    this.initJobLevel();
  },
};
</script>
<style>
</style>